package Connection;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.lang.Runnable;
import java.lang.Thread;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private ServerSocket server;
    private int port = 7777;
    public Server() {
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server example = new Server();
        example.handleConnection();
    }

    public void handleConnection() {
        System.out.println("Waiting for client message...");

        //
        // The server do a loop here to accept all connection initiated by the
        // client application.
        //
        while (true) {
            try {
                Socket socket = server.accept();
                new ConnectionHandler(socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class ConnectionHandler implements Runnable {
    private Socket socket;

    public ConnectionHandler(Socket socket) {
        this.socket = socket;

        Thread t = new Thread(this);
        t.start();
        
    }

	public void run() {
        try
        {
            //
            // Read a message sent by client application
            //
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            String Value1 = (String) ois.readObject();
            String Value2 = (String) ois.readObject();
            String Value3 = (String) ois.readObject();
            String Value4 = (String) ois.readObject();
            String Value5 = (String) ois.readObject();
            String Value6 = (String) ois.readObject();
            String Value7 = (String) ois.readObject();
            String Value8 = (String) ois.readObject();
            String Value9 = (String) ois.readObject();
            String Value10 = (String) ois.readObject();
              
            // Send a response information to the client application
            //
            database s=new database();
            //s.connection();
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            String condition="";
				condition=s.checkLogin(Value1, Value2,Value3);
				oos.writeObject(condition+"");
                oos.flush();
				if(Value3.equals("Admin"))
                {   if(Value4.equals("showTopic"))
                {
				 	database conTent=new database();
	                conTent.topicsTable();
	                oos.writeObject(conTent.getTopicName().size()+"");
	                for(int i=0;i<conTent.getTopicName().size();i++)
	                {
	                oos.writeObject(conTent.getTopicName().get(i));
	                }
	                   
	                
                }  	
            	  if(Value4.equals("ShowData"))
                 {
                	 s.questionTableAll();
                     oos.writeObject(s.getContent().size()+"");
                     for(int i=0;i<s.getContent().size();i++)
                     {
                        	oos.writeObject(s.getContent().get(i));
                        	
                     }
                  
                  
                 }
            	  if(Value4.equals("showAnswer"))
                  {
                 	 s.showConTentAnswer();
                      oos.writeObject(s.getContentAnswer().size()+"");
                      for(int i=0;i<s.getContentAnswer().size();i++)
                      {
                         	oos.writeObject(s.getContentAnswer().get(i));
                         	
                      }
                      s.questionTableAll();
  	                  oos.writeObject(s.getId().size()+"");
  		         
                   
                   
                  }
            	  if(Value4.equals("showUser"))
                  {
                 	 s.showUser();
                      oos.writeObject(s.getnameAcc().size()+"");
                      oos.writeObject(s.getpassAcc().size()+"");
                      oos.writeObject(s.getRoleAcc().size()+"");
                       
                      for(int i=0;i<s.getnameAcc().size();i++)
                      {
                         	oos.writeObject(s.getnameAcc().get(i));
                         	
                      }
                      for(int i=0;i<s.getpassAcc().size();i++)
                      {
                         	oos.writeObject(s.getpassAcc().get(i));
                         	
                      }
                      for(int i=0;i<s.getRoleAcc().size();i++)
                      {
                         	oos.writeObject(s.getRoleAcc().get(i));
                         	
                      }
                   
                   
                  }
            	  if(Value4.equals("UpdateUser"))
                  {
                 	 s.updateUser(Value5, Value6, Value7, Value8);
                     
                   
                   
                  }
            	  if(Value4.equals("deleteUser"))
                  {
                 	 s.deleteuUsers(Value5);
                     
                   
                   
                  }
             	 
            	 
            	  if(Value4.equals("showCorrectAnswer"))
                  {
                 	 s.showCorrectAnswer(Value5);
                      oos.writeObject(s.showPositionCorrect.get(0));
                     
                   
                   
                  }
             	  if(Value4.equals("InsertNewUser"))
                  {
                 	 s.insertNewUser(Value1, Value2, Value5);
                     
                   
                   
                  }
           
               	  if(Value4.equals("InsertNewTopic"))
                  {
                 
               		  
               		  s.insertTopic(Value2);
                   
                   
                  }
           
            	  
            	  if(Value4.equals("UpdateAnswers"))
                  {
            		  s.updateAnswer(Value5,Value6,Value7,Value8,Value9,Value10);
            			 
                   
                  }
                  if(Value4.equals("Insert"))
                  {
                  s.insertQuestion(Value2,Value5);
                  }
                  if(Value4.equals("InsertData"))
                  {
                 	s.insertAnswer(Value1, Value2, Value5, Value6,Value7,Value8);  
                  }
                  if(Value4.equals("UpdateData"))
                  { 
             		
                 	s.updateQuestion(Value5, Value6);
                 
                   
                  }
                  if(Value4.equals("UpdateTopic"))
                  { 
             		
                 	s.updateTopic(Value5, Value6);
                 
                   
                  }
             	  
                  if(Value4.equals("DeleteData"))
                  { 
             		
                 	s.deleteQuestion(Value5);
                 
                   
                  }
                  if(Value4.equals("DeleteTopic"))
                  { 
             		
                 	s.deleteTopic(Value5);
                 
                   
                  }
                  if(Value4.equals("showResult"))
                  { 
                	   
                 	s.showResult();
                 	oos.writeObject(s.getStudentName().size()+"");
                   	oos.writeObject(s.getStudentResult().size()+"");
                  	oos.writeObject(s.getSubject().size()+"");
                       
                 	for(int i=0;i<s.getStudentName().size();i++)
                    {
                       	oos.writeObject(s.getStudentName().get(i));
                    }
                    for(int i=0;i<s.getStudentResult().size();i++)
                    {
                       	oos.writeObject(s.getStudentResult().get(i));
                    }
                    for(int i=0;i<s.getSubject().size();i++)
                    {
                       	oos.writeObject(s.getSubject().get(i));
                    }
                         
                   
                  }
             	
             
                }
    
            
            if(condition=="Student")
            {
            if(Value4.equals("updateResultStudent"))
            {
            	database con=new database();
            	con.updateResult(Value6,Value5,Value7,Value8);
            }
            
            else
            	{
            	database conTent=new database();
            	
                conTent.topicsTable();
                conTent.randomContent(Value5);
                conTent.randomAnswer(Value5);
                conTent.checkAnswer(Value5);  
                oos.writeObject(conTent.getContent().size()+"");
                oos.writeObject(conTent.getAnswer().size()+"");
                oos.writeObject(conTent.getCorrect().size()+"");
                oos.writeObject(conTent.getTopicName().size()+"");
                
                for(int i=0;i<conTent.getContent().size();i++)
                {
                   	oos.writeObject(conTent.getContent().get(i));
                }
               
                for(int i=0;i<conTent.getAnswer().size();i++)
                {
                    	
                oos.writeObject(conTent.getAnswer().get(i));
                }
                for(int i=0;i<conTent.getCorrect().size();i++)
                {
                oos.writeObject(conTent.getCorrect().get(i));
                }
                for(int i=0;i<conTent.getTopicName().size();i++)
                {
                oos.writeObject(conTent.getTopicName().get(i));
                }
            	}
                System.out.println("Connect is successful...");
           
            }
            
            oos.flush();
            ois.close();
            oos.close();
            socket.close();
            System.out.println("Waiting for client message...");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}